package org.example.es.index;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.indices.GetIndexResponse;
import co.elastic.clients.elasticsearch.indices.GetMappingResponse;
import org.example.ElasticClient;

import java.io.IOException;

/**
 * @Author : 叶荻风
 * @create : 2024/6/6 10:27
 */
public class CreateIndex {
    static ElasticsearchClient client = ElasticClient.getClient();

    public static void main(String[] args) throws IOException {
        addIndex("products");
        getIndex("products");
       // getMapping();
        getMapping("products");
        client._transport().close();
    }

    public static void addIndex(String indexName) throws IOException {
        boolean value = client.indices().exists(builder -> builder.index(indexName)).value();
        if(!value){
            client.indices().create(builder -> builder.index(indexName));
        }else{
            System.out.println("此index已经存在");
        }
    }


    public static void getIndex(String indexName) throws IOException {
        GetIndexResponse getIndexResponse = client.indices().get(c -> c.index(indexName));
        System.out.println(getIndexResponse.result().toString());
    }
    public static void getMapping() throws IOException {
        GetMappingResponse mapping = client.indices().getMapping();
        System.out.println(mapping.result().toString());
    }
    public static void getMapping(String indexName) throws IOException {
        GetMappingResponse mapping = client.indices().getMapping(m -> m.index(indexName));
        System.out.println(mapping.result().toString());
    }

    public static void addDocument(Object o){



    };

}
